package com.project.service;

import com.project.entity.CarGoods;
import com.project.entity.Order;
import com.project.mapper.CarGoodsMapper;
import com.project.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarGoodsService {
    @Autowired
    private CarGoodsMapper carGoodsMapper;

    public List<CarGoods> findPage(Integer pageIndex, Integer pageItemCount){

        Integer first = (pageIndex-1)*pageItemCount;

        return carGoodsMapper.findPage(first,pageItemCount);
    }


    public List<CarGoods> findAll(String userName){
        return carGoodsMapper.findAll(userName);
    }

    public String save(CarGoods carGoods,String userName){


        //查询购物车中是否有此商品，有则只改数量即可，没有则添加
        CarGoods carGood = carGoodsMapper.findOneByOrderId(carGoods.getOrderId(),userName);
        //System.out.println(carGood);
        if(carGood != null){
            //改数量
            carGoodsMapper.updateCountByOrderId(1,carGood.getOrderId(),carGood.getOwnName());
            return "你购物车已有此商品，修改数量成功";
        }else {
            //根据商品id查询
            carGoodsMapper.save(carGoods);
            return "添加到购物车成功";
        }
    }

    public void deleteById(Integer id){
        carGoodsMapper.deleteById(id);
    }

    public void updateCount(Integer count,Integer shoppingId ){
        carGoodsMapper.updateCount(count,shoppingId);
    }

 }
